//
//  LSViewController.m
//  yiYuanDuoBao
//
//  Created by 罗李苏 on 16/11/21.
//  Copyright © 2016年 罗李苏. All rights reserved.
//

#import "LSViewController.h"

#import "shouYeMainViewController.h"

#import "jieXiaoMainViewController.h"

#import "qingDanMainViewController.h"

#import "mySelfMainViewController.h"
///自定义的tabBar
#import "LS_TabBar.h"
#import "tabBarBottomView.h"
#import "tabBatBtn.h"
#import "BaseNavigationViewController.h"
#import "UIColor+CBJExtention.h"
@interface LSViewController ()<LS_TabBarDelegate>

@end

@implementation LSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   //给tabBarController添加子控制器
    [self addSubVC];
   
    
    // 添加底部bar 注意：这里要先添加子控制器再添加底部自定义barView  不然barView会被系统的盖住
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self addBottomView];
//    });
    
    //创建自定义的tabBar
    LS_TabBar *tabBar = [[LS_TabBar alloc] init];
    //设置代理
    tabBar.MyDelegate = self;
#warning 注意必须使用KVC的方式才能把自定义的tabBar替换掉系统自带的
    [self setValue:tabBar forKey:@"tabBar"];
}
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
}
- (void)addSubVC{

//    shouYeViewController *shouYeNav = [[shouYeViewController alloc] initWithRootViewController:[[shouYeMainViewController alloc] init]];
//    jieXiaoViewController *jieXiaoNav = [[jieXiaoViewController alloc] initWithRootViewController:[[jieXiaoMainViewController alloc] init]];
//    qingDanViewController *qingDanNav = [[qingDanViewController alloc] initWithRootViewController:[[qingDanMainViewController alloc] init]];
//    mySelfViewController *mySelfNav = [[mySelfViewController alloc] initWithRootViewController:[[mySelfMainViewController alloc] init]];
    mySelfMainViewController *myVc = [[mySelfMainViewController alloc] init];
    qingDanMainViewController *qingVc = [[qingDanMainViewController alloc] init];
    jieXiaoMainViewController *jieVc = [[jieXiaoMainViewController alloc] init];
    shouYeMainViewController *shouVc = [[shouYeMainViewController alloc] init];
//    [self addChildViewController:shouYeNav];
//    [self addChildViewController:jieXiaoNav];
//    [self addChildViewController:qingDanNav];
//    [self addChildViewController:mySelfNav];
    [self setUpOneChildVcWithVc:shouVc Image:@"标签栏_首页_默认状态_icon" SelectedImage:@"标签栏_首页_选中状态_icon" title:@"首页"];
    [self setUpOneChildVcWithVc:jieVc Image:@"标签栏_即将揭晓_默认状态_icon" SelectedImage:@"标签栏_即将揭晓_选中状态_icon" title:@"即将揭晓"];
    [self setUpOneChildVcWithVc:qingVc Image:@"标签栏_清单_正常状态_icon" SelectedImage:@"标签栏_清单_选中状态_icon" title:@"清单"];
    [self setUpOneChildVcWithVc:myVc Image:@"标签栏_我的_默认状态_icon" SelectedImage:@"标签栏_我的_选中状态_icon" title:@"我的"];
}
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)normalImage SelectedImage:(NSString *)selectedImage title:(NSString *)title{

    BaseNavigationViewController *baseNav = [[BaseNavigationViewController alloc] initWithRootViewController:Vc];
    UIImage *norImage = [UIImage imageNamed:normalImage];
    norImage = [norImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Vc.tabBarItem.image = norImage;
    
    UIImage *seleImage = [UIImage imageNamed:selectedImage];
    seleImage = [seleImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Vc.tabBarItem.selectedImage = seleImage;
    
    NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary];
    selectedDict[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selectedDict[NSForegroundColorAttributeName] = [UIColor colorWithString:@"FF2104"];
#warning 好恶心的设置方法
    [Vc.tabBarItem setTitleTextAttributes:selectedDict forState:UIControlStateSelected];
    
    baseNav.navigationItem.title = title;
    Vc.tabBarItem.title = title;
    
    [self addChildViewController:baseNav];
    
//    Vc.tabBarItem.selectedImage
}
//实现自定义tabBar中特殊按钮的代理方法
- (void)tabBarPlusBtnClick:(UITabBar *)tabBar{

    NSLog(@"赵日天在此,谁敢不服");
}
- (void)addBottomView{

    tabBarBottomView *bottonView = [[tabBarBottomView alloc] initWithFrame:self.tabBar.bounds];
    [self.tabBar addSubview:bottonView];
    
    //实现Block
    bottonView.go2VcBlock = ^(NSInteger index){
    
        self.selectedIndex = index;
        
    };
    //默认选中第0个按钮
    [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        tabBatBtn *btn = (tabBatBtn *)[self addButton:idx];
        [bottonView addSubview:btn];
        
        if (idx == 0) {
            [bottonView btnClick:btn];
        }
    }];
}

//MARK:给自定义的bottomView添加按钮的方法
- (tabBatBtn *)addButton: (NSInteger)index{
    tabBatBtn *btn = [[tabBatBtn alloc]init];
    //添加btn
//    tabBatBtn *btn = [[tabBatBtn alloc] init];
    if (index == 0) {
//       [btn buttonWithImageNmae:@"标签栏_首页_默认状态_icon" text:@"首页"];
        [btn setImage:[UIImage imageNamed:@"标签栏_首页_默认状态_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"标签栏_首页_选中状态_icon"] forState:UIControlStateSelected];
//        [btn setTitle:@"首页" forState:UIControlStateNormal];
        btn.titleLabel.text = @"首页";
        return btn;
        
    }else if (index == 1){
        
//        [btn buttonWithImageNmae:@"标签栏_即将揭晓_默认状态_icon" text:@"即将揭晓"];

        [btn setImage:[UIImage imageNamed:@"标签栏_即将揭晓_默认状态_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"标签栏_即将揭晓_选中状态_icon"] forState:UIControlStateSelected];
        btn.titleLabel.text = @"即将揭晓";

        return btn;

    }else if (index == 2){
    
//        [btn buttonWithImageNmae:@"标签栏_清单_正常状态_icon" text:@"清单"];
        [btn setImage:[UIImage imageNamed:@"标签栏_清单_正常状态_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"标签栏_清单_选中状态_icon"] forState:UIControlStateSelected];
//        [btn setTitle:@"清单" forState:UIControlStateNormal];
        btn.titleLabel.text = @"清单";

        return btn;

    }else{
    
//        [btn buttonWithImageNmae:@"标签栏_我的_默认状态_icon" text:@"我的"];
        [btn setImage:[UIImage imageNamed:@"标签栏_我的_默认状态_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"标签栏_我的_选中状态_icon"] forState:UIControlStateSelected];
//        [btn setTitle:@"我的" forState:UIControlStateNormal];
        btn.titleLabel.text = @"我的";

        return btn;

    }
//    return btn;
}
@end
